window.addEventListener("load", function () {
    var btn1 = document.querySelector(".btn1");
    var btn2 = document.querySelector(".btn2");
    var div = document.querySelector("div");


    btn1.addEventListener('click', function () {
        animate(div, 500);
    })
    btn2.addEventListener('click', function () {
        animate(div, 300);
    })
    function animate(obj, target, callback) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function () {
            var step = (target - div.offsetLeft) / 10;
            step = step < 0 ? Math.floor(step) : Math.ceil(step);
            if (div.offsetLeft == target) {
                clearInterval(obj.timer);
                timer = null;
                callback && callback();
            }
            obj.style.left = obj.offsetLeft + step + 'px'
        }, 15)
    }

})